using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMathLibrary.Management
{
    /// <summary>
    /// This class provides to build up a object pool, which  contains ready object instances to use.
    /// </summary>
    /// <typeparam name="T">The type of the instances in the object pool.</typeparam>
    [Serializable]
    public class ObjectPool<T>
    {
        /// <summary>
        /// Saves the maximum number of pool objects.
        /// </summary>
        private readonly int poolSize;

        /// <summary>
        /// The queue for the pool objects.
        /// </summary>
        private readonly Queue<T> poolObjects;

        /// <summary>
        /// The parameters for initializing a new pool object.
        /// </summary>
        private readonly Object[] instanceArguments;

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPool&lt;T&gt;"/> class.
        /// </summary>
        public ObjectPool()
        {
            this.poolSize = 25;
            this.poolObjects = new Queue<T>(this.poolSize);
            this.instanceArguments = new Object[0];

            this.RefillPool();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="poolSize">The size of the object pool.</param>
        public ObjectPool(int poolSize)
        {
            this.poolSize = poolSize;
            this.poolObjects = new Queue<T>(this.poolSize);
            this.instanceArguments = new Object[0];

            this.RefillPool();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="instanceArguments">The arguments to create the needed instances of the 
        /// objects in the pool.</param>
        public ObjectPool(Object[] instanceArguments)
        {
            this.poolSize = 25;
            this.poolObjects = new Queue<T>(this.poolSize);
            this.instanceArguments = instanceArguments;

            this.RefillPool();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="poolSize">The size of the object pool.</param>
        /// <param name="instanceArguments">The arguments to create the needed instances of the 
        /// objects in the pool.</param>
        public ObjectPool(int poolSize, Object[] instanceArguments)
        {
            this.poolObjects = new Queue<T>(this.poolSize);
            this.instanceArguments = instanceArguments;

            this.RefillPool();
        }

        /// <summary>
        /// Gets the number of objects, which are currently available in the object pool.
        /// </summary>
        /// <value>The number of objects, which are currently available in the object pool.</value>
        public int Count
        {
            get { return this.poolObjects.Count; }
        }

        /// <summary>
        /// Gets the arguments to create the needed instances of the objects in the pool.
        /// </summary>
        /// <value>The arguments to create the needed instances of the objects in the pool.</value>
        public Object[] InstanceArguments
        {
            get { return instanceArguments; }
        }

        /// <summary>
        /// Retrieves a pool object out of the pool. If the pool contains no objects any more, the 
        /// method will initialize and return a new one.
        /// </summary>
        /// <returns>An object out of the object pool.</returns>
        public T RetrievePoolItem()
        {
            if (this.poolObjects.Count > 0)
            {
                return this.poolObjects.Dequeue();
            }

            return (T) Activator.CreateInstance(typeof (T), instanceArguments);
        }

        /// <summary>
        /// Refills the object pool with new objects.
        /// </summary>
        public void RefillPool()
        {
            for (int i = this.poolObjects.Count; i < this.poolSize; i++)
            {
                this.poolObjects.Enqueue((T) Activator.CreateInstance(typeof (T), instanceArguments));
            }
        }

        /// <summary>
        /// Destroys the object pool.
        /// </summary>
        public void DestroyPool()
        {
            this.poolObjects.Clear();
        }
    }
}